package com.ruike.alisurface.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.ruike.alisurface.BuildConfig;
import com.ruike.alisurface.Constant;
import com.ruike.alisurface.MyApplication;
import com.voodoo.lib_utils.L;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Map;

import ZtlApi.ZtlManager;

import static android.content.Context.TELEPHONY_SERVICE;

/**
 * Author: voodoo
 * CreateDate: 2020-03-26 026 上午 10:58
 * Description: 设备相关工具类
 */
public class DeviceUtils {

    /**
     * 设置SN
     */
    public static void setZtlDeviceSn(String snStr) {
//        ZtlManager.GetInstance().setBuildSerial((snStr == null || snStr.isEmpty()) ? "RKAI2MNYLBDU1JU95437610" : snStr);
    }

    /**
     * 获取机器SN
     *
     * @return 机器sn
     */
    public static String getDeviceSn() {
        setZtlDeviceSn("");
        String serial = null;
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class);
            serial = (String) get.invoke(c, "ro.serialno");
        } catch (Exception e) {
            e.printStackTrace();
        } // 003001314210b188e37b   RKAI2POXA4FOB2RTEST0001
//        return BuildConfig.DEBUG ? "801001408000b4f741ec" : serial;
        return serial;
    }

    @SuppressLint({"MissingPermission", "HardwareIds"})
    public static String getSimSerialNumber() {
        String iccId = "";
        try {
            TelephonyManager telMgr = (TelephonyManager) MyApplication.getAppContext().getSystemService(TELEPHONY_SERVICE);
            iccId = telMgr.getSimSerialNumber();
            if (iccId.isEmpty() || iccId.length() < 10) {
                iccId = getICCID().trim();
            }
        } catch (Exception e) {
            L.e(e.getMessage());
        }
        return iccId;
    }

    public static String getICCID() {
        String iccid = "";
        String file = "/dev/ttyUSB1";
        String cmd = "at+qccid";
        RandomAccessFile localRandomAccessFile = null;
        try {
            localRandomAccessFile = new RandomAccessFile(file, "rw");
            localRandomAccessFile.writeBytes(cmd + "\r\n");
            localRandomAccessFile = new RandomAccessFile(file, "r");
            byte[] arrayOfByte = new byte[1024];
            int readSize = 0;
            while ((readSize = localRandomAccessFile.read(arrayOfByte)) == -1) {
            }
            localRandomAccessFile.close();
            iccid = new String(arrayOfByte).substring(0, readSize);
            L.i("原始数据：", iccid);
            iccid = iccid.substring(iccid.indexOf(":") + 1, iccid.indexOf(":") + 22);
            L.i("iccid:", iccid);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return iccid;
    }

    /**
     * 导航键和导航栏的隐藏
     *
     * @param isShow 是否显示导航栏
     * @return 设置结果，true：设置成功， false：设置失败
     */
    public static boolean showNavigationBar(boolean isShow) {
        try {
            String command;
            if (isShow) {
                command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib am startservice -n com.android.systemui/.SystemUIService";
            } else {
                command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib service call activity 42 s16 com.android.systemui";
            }
            ArrayList<String> envlist = new ArrayList<>();
            Map<String, String> env = System.getenv();
            for (String envName : env.keySet()) {
                envlist.add(envName + "=" + env.get(envName));
            }
            String[] envp = envlist.toArray(new String[0]);
            Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", command}, envp);
            proc.waitFor();
            return true;
        } catch (Exception ex) {
            L.e("设置导航栏显隐失败：" + ex.toString());
            return false;
        }
    }

}
